package com.ihaveu.android.dotateam;

import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.Random;

/**
 * 目标  20人中随机分成4队  每队的平均分差不多
 */
public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private ArrayList<Doter> mList = new ArrayList<>();
    private Button mBtn;
    private TextView mTeamA,mTeamB,mTeamC,mTeamD;
    private ArrayList<Doter> mListA = new ArrayList<>();
    private ArrayList<Doter> mListB = new ArrayList<>();
    private ArrayList<Doter> mListC = new ArrayList<>();
    private ArrayList<Doter> mListD = new ArrayList<>();
    private int doterSize = 20;
    private int teamScore = 0;
    private int meanTeamScore = 0;
    private Random random = new Random();
    private boolean A,B,C,D;
    /**
     * 阻止郭神和志云在一起的次数
     */
    private int num;
    Doter doterGaoshu;
    Doter doterWenge;
    Doter doterTiankong;
    Doter doterGuoshen;
    Doter doterWencai;
    Doter doterXiaocai;
    Doter doterXiangge;
    Doter doterZhiyun;
    Doter doterCYM;
    Doter doterDavy;
    Doter doterSir;
    Doter doterAlu;
    Doter doterLiuPing;
    Doter doterXiaozhuang;
    Doter doterDoom;
    Doter doterErSHEN;
    Doter doterXiaoxuesheng;
    Doter doterAige;
    Doter doterKongzhizhen;
    Doter doter08;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });


        mBtn = (Button) findViewById(R.id.button);
        mBtn.setOnClickListener(this);
        mTeamA = (TextView) findViewById(R.id.teamA);
        mTeamB = (TextView) findViewById(R.id.teamB);
        mTeamC = (TextView) findViewById(R.id.teamC);
        mTeamD = (TextView) findViewById(R.id.teamD);

        //1
        doterGaoshu = new Doter();
        doterGaoshu.setName("高叔");
        doterGaoshu.setScore(1050);
        mList.add(doterGaoshu);
        //2
        doterWenge = new Doter();
        doterWenge.setName("G");
        doterWenge.setScore(700);
        mList.add(doterWenge);
        //3
        doterTiankong = new Doter();
        doterTiankong.setName("Sky");
        doterTiankong.setScore(1181);
        mList.add(doterTiankong);
        //4
        doterGuoshen = new Doter();
        doterGuoshen.setName("郭");
        doterGuoshen.setScore(1200);
        mList.add(doterGuoshen);
        //5
        doterWencai = new Doter();
        doterWencai.setName("井");
        doterWencai.setScore(1500);
        mList.add(doterWencai);
        //6
        doterXiaocai = new Doter();
        doterXiaocai.setName("小菜");
        doterXiaocai.setScore(1402);
        mList.add(doterXiaocai);
        //7
        doterXiangge = new Doter();
        doterXiangge.setName("鸿洋");
        doterXiangge.setScore(1401);
        mList.add(doterXiangge);
        //8
        doterZhiyun = new Doter();
        doterZhiyun.setName("志云");
        doterZhiyun.setScore(965);
        mList.add(doterZhiyun);
        //9
        doterCYM = new Doter();
        doterCYM.setName("CYM");
        doterCYM.setScore(1000);
        mList.add(doterCYM);
        //10
        doterDavy = new Doter();
        doterDavy.setName("NB");
        doterDavy.setScore(800);
        mList.add(doterDavy);
        //11
        doterSir = new Doter();
        doterSir.setName("Sir");
        doterSir.setScore(300);
        mList.add(doterSir);
        //12
        doterAlu = new Doter();
        doterAlu.setName("Alu");
        doterAlu.setScore(1100);
        mList.add(doterAlu);
        //13
        doterLiuPing = new Doter();
        doterLiuPing.setName("liuping");
        doterLiuPing.setScore(1000);
        mList.add(doterLiuPing);
        //14
        doterXiaozhuang = new Doter();
        doterXiaozhuang.setName("小庄");
        doterXiaozhuang.setScore(1200);
        mList.add(doterXiaozhuang);
        //15
        doterDoom = new Doter();
        doterDoom.setName("爱上表嫂");
        doterDoom.setScore(1400);
        mList.add(doterDoom);
        //16
        doterErSHEN = new Doter();
        doterErSHEN.setName("毛");
        doterErSHEN.setScore(1500);
        mList.add(doterErSHEN);
        //17
        doterXiaoxuesheng = new Doter();
        doterXiaoxuesheng.setName("小学生");
        doterXiaoxuesheng.setScore(600);
        mList.add(doterXiaoxuesheng);
        //18
        doterAige = new Doter();
        doterAige.setName("愛哥");
        doterAige.setScore(1200);
        mList.add(doterAige);
        //19
        doterKongzhizhen = new Doter();
        doterKongzhizhen.setName("nullpointer");
        doterKongzhizhen.setScore(1000);
        mList.add(doterKongzhizhen);
        //20
        doter08 = new Doter();
        doter08.setName("08");
        doter08.setScore(1500);
        mList.add(doter08);

        int totalScore = 0;
        for(Doter item : mList){
            totalScore += item.getScore();
        }
        //队伍的平均分数
        meanTeamScore = totalScore / 4;
        mBtn.setText(mBtn.getText().toString() + "队伍均值分为 ："+meanTeamScore);
        Log.i("ghq","" + new Random().nextInt(2));
    }

    /**
     * 获取正确的队伍
     */
    private void getCurrentTeam(ArrayList<Doter> team,TextView textView,boolean current,String s){
        while (true){
            getTeam(team);
            if(Math.abs((teamScore - meanTeamScore)) < 25){
                showTeamInfo(team,textView,s);
                current = true;
            }else if(Math.abs((teamScore - meanTeamScore)) < 50){
                showTeamInfo(team,textView,s);
                current = true;
            }else if(Math.abs((teamScore - meanTeamScore)) < 100){
                showTeamInfo(team,textView,s);
                current = true;
            }else {
                mList.addAll(team);
                current = false;
                doterSize += 5;
                teamScore = 0;
            }
            if(current){
                teamScore = 0;
                break;
            }
        }
    }

    /**
     * 获取队伍
     * @param team
     */
    private void getTeam(ArrayList<Doter> team){
        teamScore = 0;
        team.removeAll(team);
        for(int num=0;num<5;num++){
            int position = random.nextInt(doterSize);
            int doterScore = mList.get(position).getScore();
            teamScore += doterScore;
            doterSize -= 1;
            team.add(mList.remove(position));
        }
    }

    /**
     * 显示队伍信息
     * @param
     * @return
     */
    private void showTeamInfo(ArrayList<Doter> team,TextView textView,String s){
        int tempteamScore = 0;
        for(Doter item : team){
            tempteamScore += item.getScore();
        }
        textView.setText(s + "队有 ：" + team.get(0).getName() + "("+"天梯积分" + team.get(0).getScore()+")" + " ,"
                + team.get(1).getName() +"("+"天梯积分" + team.get(1).getScore()+")"  +" ,"
                + team.get(2).getName() +"("+"天梯积分" + team.get(2).getScore()+")"  +" ,"
                + team.get(3).getName()+ "("+"天梯积分" + team.get(3).getScore()+")"  + " ,"
                + team.get(4).getName()+ "("+"天梯积分" + team.get(4).getScore()+")"  + " ." +
                " \n队伍总分是 :" + tempteamScore + "\n 队伍人数为 ：" + team.size() + "\n 队伍个人平均分数 ：" + tempteamScore / 5
        + "\n为了保证郭神和志云不在一个分组共额外执行了" +num + "次。");
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @Override
    public void onClick(View view) {

        if(mList.size() < 20){
            mList.addAll(mListA);
            mList.addAll(mListB);
            mList.addAll(mListC);
            mListD.removeAll(mListD);
        }

        doterSize = 20;
        teamScore = 0;
        getCurrentTeam(mListA, mTeamA, A, "A");
        getCurrentTeam(mListB, mTeamB, B, "B");
        getCurrentTeam(mListC, mTeamC, C, "C");

        mListD.addAll(mList);

        if (mListA.contains(doterGuoshen) && mListA.contains(doterZhiyun)){
            num += 1;
            onClick(mBtn);
        }
        else if(mListB.contains(doterGuoshen) && mListB.contains(doterZhiyun)) {
            num += 1;
            onClick(mBtn);
        }
        else if(mListC.contains(doterGuoshen) && mListC.contains(doterZhiyun)) {
            num += 1;
            onClick(mBtn);
        }
        else if(mListD.contains(doterGuoshen) && mListD.contains(doterZhiyun)) {
            num += 1;
            onClick(mBtn);
        }


        showTeamInfo(mListD, mTeamD, "D");
        int teamScore = 0;
        for(Doter item : mListD){
            teamScore += item.getScore();
        }
        if(Math.abs((teamScore - meanTeamScore)) > 100){
            mList.addAll(mListA);
            mList.addAll(mListB);
            mList.addAll(mListC);
            mListD.removeAll(mListD);
            onClick(mBtn);
        }
    }
}
